/* 
 -----------------------------------------------------------------------------
                   Cogaen - Component-based Game Engine (v3)
 -----------------------------------------------------------------------------
 This software is developed by the Cogaen Development Team. Please have a 
 look at our project home page for further details: http://www.cogaen.org
    
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Copyright (c) 2010 Roman Divotkey

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 */

package org.cogaen.java2d;

import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import java.util.List;

public class SceneNode {
	private String name;
	private ArrayList<SceneNode> children;
	private ArrayList<Visual> visuals = new ArrayList<Visual>();

	private AffineTransform transform = new AffineTransform();
	private double angle;

	public SceneNode(String name) {
		this.name = name;
	}

	public SceneNode() {
		// intentionally left empty
	}

	public String getName() {
		return this.name;
	}

	AffineTransform getTransform() {
		return this.transform;
	}

	public void addChild(SceneNode childNode) {
		// lazy initialization
		if (this.children == null) {
			this.children = new ArrayList<SceneNode>();
		}

		this.children.add(childNode);
	}

	/**
	 * With this function the z order of the sprite can be set.
	 * <p>
	 * The higher the zOrder value, the more levels the sprite is on top, so a
	 * sprite with zOrder 1 is drawn in front of a sprite with zOrder 0.
	 * </p>
	 * 
	 * @param childNode
	 *            SceneNode to add as a child node.
	 * @param zOrder
	 *            Index where this child should be added to the children list.
	 *            Must be a positive integer. The children with the highest
	 *            index are drawn on top.
	 */
	public void addChild(SceneNode childNode, int zOrder) {
		// lazy initialization
		if (this.children == null) {
			this.children = new ArrayList<SceneNode>();
		}

		this.children.add(zOrder, childNode);
	}

	public void addVisual(Visual visual) {
		if (visual != null) {
			this.visuals.add(visual);
		}
	}
	
	public void removeVisual(Visual visual) {
		this.visuals.remove(visual);
	}
	
	public void clearVisuals() {
		this.visuals.clear();
	}
		
	public List<SceneNode> getChildren() {
		return this.children;
	}

	public List<Visual> getVisuals() {
		return this.visuals;

	}

	public void setPose(double x, double y, double angle) {
		this.transform.setToIdentity();
		// adjust to java2d's coordinate system and flip y-coordinate and angle
		this.transform.translate(x, -y);
		this.transform.rotate(-angle);
		this.angle = angle;
	}

	public double getPositionX() {
		return this.transform.getTranslateX();
	}

	public double getPositionY() {
		return -this.transform.getTranslateY();
	}
	
	public double getAngle() {
		return this.angle;
	}
	
}
